class Solutoin02 {
	public int pathSum(TreeNode root, int targetSum) {
		if(root == null){
			return 0;
		}
		int pathNum = rootSum(root, targetSum);
		pathNum += pathSum(root.left, targetSum);
		pathNum += pathSum(root.right, targetSum);
		return pathNum;
	}

	public int rootSum(TreeNode root, int targetSum){
		int curNum = 0;
		if(root == null){
			return curNum;
		}
		if(root.val == targetSum){
			curNum++;
		}
		// 遍历子树时，更新targetSum
		curNum += rootSum(root.left, targetSum - root.val);
		curNum += rootSum(root.right, targetSum - root.val);
		return curNum;
	}
}
